package com.fetch.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.Timer;

import com.download.entity.DownloadInfo;
import com.download.utils.DownloadHelper;

public class DownloadActionListener implements ActionListener {
    
    private JTextField url;
    private JTextField fileName;
    private JTextField directory;
    private JProgressBar progressBar;
    private JButton openFile;
    private JButton openFolder;
    
    public DownloadActionListener(JTextField url, JTextField fileName,
            JTextField directory, JProgressBar progressBar, JButton openFile,
            JButton openFolder) {
        super();
        this.url = url;
        this.fileName = fileName;
        this.directory = directory;
        this.progressBar = progressBar;
        this.openFile = openFile;
        this.openFolder = openFolder;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        DownloadInfo downloadInfo = DownloadHelper.createDownloadInfo(url.getText().trim(), fileName.getText().trim(), directory.getText().trim(), DownloadHelper.SPLITTER_NUM);
        DownloadHelper.executeDownload(downloadInfo);
        progressBar.setValue(0);
        Timer timer = new Timer(10000, new FinishActionListener(progressBar, openFile, openFolder));
        timer.start();
    }

}
